/**
 *
 * User: yuml
 * Date: 11-10-30
 * Time: 下午4:21
 */
Ext.define('Users', {
    extend: 'Ext.data.Model',
    fields: [
        'userName','nickName','sex','userState','email','mobile'
    ]
});

function createUserWindow() {


    var editUserWindow = Ext.create('Ext.Window', {
        title: 'Right Header, plain: true',
        width: 400,
        height: 300,
        layout: 'fit',
        items: [
            {
                xtype:'form',
                id:'userForm',
                bodyPadding: 5,
                // The form will submit an AJAX request to this URL when submitted
                url: '/user/edit.do',
                method:'post',
                // Fields will be arranged vertically, stretched to full width
                layout: 'anchor',
                defaults: {
                    anchor: '100%'
                },
                // The fields
                defaultType: 'textfield',
                items: [
                    {
                        fieldLabel: '用户名',
                        name: 'userName',
                        allowBlank: false
                    },
                    {
                        fieldLabel: '密码',
                        name: 'password',
                        allowBlank: false
                    },
                    {
                        fieldLabel: '昵称',
                        name: 'nickName',
                        allowBlank: false
                    },
                    {
                        fieldLabel: '性别',
                        name: 'sex',
                        allowBlank: false
                    },
                    {
                        fieldLabel: '邮箱',
                        name: 'email',
                        allowBlank: false
                    },
                    {
                        fieldLabel: '移动电话',
                        name: 'mobile',
                        allowBlank: false
                    }
                ],

                // Reset and Submit buttons
                buttons: [
                    {
                        text: '提交',
                        formBind: true, //only enabled once the form is valid
                        disabled: true,
                        handler: function() {
                            var form = this.up('form').getForm();
                            if (form.isValid()) {
                                form.submit({
                                    success: function(form, action) {
                                        Ext.Msg.alert('Success', action.result.msg);
                                    },
                                    failure: function(form, action) {
                                        Ext.Msg.alert('Failed', action.result.msg);
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        handler: function() {
                            this.up('form').getForm().reset();
                        }
                    }
                ]
            }
        ]
    });
    return editUserWindow;
}

Ext.define('MyDesktop.UserWindow', {
    extend: 'Ext.ux.desktop.Module',

    requires: [
        'Ext.data.Store',
        'Ext.util.Format',
        'Ext.grid.Panel',
        'Ext.grid.RowNumberer',
        'Ext.data.Model'
    ],

    id:'user-win',

    init : function() {
        this.launcher = {
            text: '用户管理',
            iconCls:'icon-grid',
            handler : this.createWindow,
            scope: this
        };
    },

    createWindow : function() {
        var desktop = this.app.getDesktop();
        var win = desktop.getWindow('user-win');
        if (!win) {
            //创建数据源
            var userStore = Ext.create('Ext.data.Store', {
                //分页大小
                pageSize: 10,
                model: 'Users',
                //是否在服务端排序
                //remoteSort: true
                proxy: {
                    type: 'ajax',
                    url : '/user/search.do',
                    reader: {
                        type:'json',
                        root:'data'
                    }
                }
            });

            userStore.on('beforeload', function (store, options) {
                // alert('beforeload');
                var extParams = search.getForm().getValues();
                Ext.apply(store.proxy.extraParams, extParams);
            });
            var search = Ext.create('Ext.form.Panel', {
                id:'searchForm',
                layout: {
                    type: 'column'
                },
                title: '搜索条件',
                bodyPadding: 10,
                defaultType:'textfield',
                items: [
                    {
                        name:'userName',
                        fieldLabel: '用户名',
                        labelAlign: 'right',
                        labelWidth: 50
                    },
                    {
                        name:'nickName',
                        fieldLabel: '昵称',
                        labelAlign: 'right',
                        labelWidth: 50
                    },
                    {
                        vtype:'email',
                        name:'email',
                        fieldLabel: '邮箱',
                        labelAlign: 'right',
                        labelWidth: 50
                    },
                    {
                        name:'mobile',
                        fieldLabel: '移动电话',
                        labelAlign: 'right',
                        labelWidth: 70
                    }
                ],
                buttons:[
                    {
                        width: 60,
                        text: '搜索',
                        type: 'submit',
                        listeners:{
                            click:function() {
                                userStore.load({start:0, limit: 10});
                            }
                        }
                    },
                    {
                        width: 60,
                        text: '重置',
                        type: 'reset',
                        listeners:{
                            click:function() {
                                search.getForm().reset();
                                userStore.load({start:0, limit: 10});
                            }
                        }
                    }
                ]

            });

            var dataList = Ext.create('Ext.grid.Panel', {
                title:'用户列表',
                store: userStore,
                height:360,
                loadMask:true,
                columns: [
                    new Ext.grid.RowNumberer(),
                    {
                        text: "用户名",
                        flex: 1,
                        sortable:false,
                        dataIndex: 'userName'
                    },
                    {
                        text: "昵称",
                        width: 200,
                        sortable:false,
                        //renderer: Ext.util.Format.usMoney,
                        dataIndex: 'nickName'
                    },
                    {
                        text: "性别",
                        width: 70,
                        sortable:false,
                        dataIndex: 'sex'
                    },
                    {
                        text: "状态",
                        width: 70,
                        sortable:false,
                        dataIndex: 'userState'
                    }
                    ,
                    {
                        text: "邮箱",
                        width: 200,
                        sortable:false,
                        dataIndex: 'email'
                    },
                    {
                        text: "移动电话",
                        width: 120,
                        sortable:false,
                        dataIndex: 'mobile'
                    }
                ],
                tbar:[
                    {
                        text:'添加用户',
                        tooltip:'添加用户',
                        iconCls:'add',
                        handler:function() {
                            createUserWindow().show();
                        }
                    },
                    '-',
                    {
                        text:'删除用户',
                        tooltip:'删除选择的用户',
                        iconCls:'remove'
                    }
                ] ,
                dockedItems: [
                    {
                        xtype: 'pagingtoolbar',
                        store: userStore,
                        dock: 'bottom',
                        displayInfo : true
                    }
                ]
            });

            win = desktop.createWindow({
                id: 'user-win',
                title:'用户管理',
                width:900,
                height:520,
                iconCls: 'icon-grid',
                animCollapse:false,
                constrainHeader:true,
                layout: 'anchor',
                defaults:{anchor:'100%'},
                items: [
                    search ,
                    dataList
                ]
            });
            win.on("afterrender", function() {
                userStore.load({start:0, limit: 10});
            });
        }
        win.show();
        return win;
    }
});